<template>
  <van-button
    v-if="isFollowed"
    class="follow-btn"
    round
    size="small"
    @click="onFollws"
    :loading="followLoading"
    >已关注</van-button
  >
  <van-button
    v-else
    class="follow-btn"
    type="info"
    color="#3296fa"
    round
    size="small"
    icon="plus"
    @click="onFollws"
    :loading="followLoading"
    >关注</van-button
  >
</template>

<script>
import { addFollow, deleteFollow } from '@/api/user'

export default {
  name: 'FollowUser',
  props: {
    isFollowed: {
      type: Boolean,
      require: true
    },
    userId: {
      type: [Number, String, Object],
      require: true
    }
  },
  model: {
    prop: 'isFollowed',
    event: 'updata-is-follow'
  },
  components: {},
  data () {
    return {
      followLoading: false
    }
  },
  created () {

  },
  methods: {
    async onFollws () {
      this.followLoading = true
      try {
        if (this.isFollowed) {
          await deleteFollow(this.userId)
        } else {
          await addFollow(this.userId)
        }
        this.$emit('updata-is-follow', !this.isFollowed)
      } catch (error) {
        if (error.response && error.response.status === 400) {
          this.$toast('不能关注自己')
        } else {
          this.$toast('其他错误')
        }
      }
      this.followLoading = false
    }
  }
}
</script>

<style lang="less" scoped>
</style>
